<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>register</title>
<script src="js/jquery-2.2.3.min.js"></script>
<script src="js/jquery-ui.min.js"></script>
<link type="text/css" href="css/jquery-ui.min.css" rel="stylesheet">
<link type="text/css" href="css/register.css" rel="stylesheet">
<link href="css/bootstrap.min.css" rel="stylesheet">
<script src="js/bootstrap.min.js"></script>
<script src="js/my.js"></script>
<script type="text/javascript">
    

    function passCheck(){ //验证密码
        var userpass = document.myform.password.value;
        if(userpass == ""){
            alert("未输入密码 \n" + "请输入密码");
            document.myform.password.focus();
            return false;   }
        if(userpass.length < 6||userpass.length>12){
            alert("密码必须在 6-12 个字符。\n");
            return false;   }
        return true;  
    }

      function codeCheck(){ //验证密码
        var code = document.getElementById("code").value;
        if(code == ""){
            alert("未输入验证码 \n" + "请输入密码");
                        return false;  
                     }
                 return true;  
    }

    
    function checkEmail(){
        var Email = document.getElementById("email").value;
        var e = Email.indexOf("@"&&".");
        if(Email.length!=0){
            if(e>0){
                if(Email.charAt(0)=="@"&&"."){
                    alert("符号@和符号.不能再邮件地址第一位");
                return false;
                }
                else{
                    return true;
                }
            }
            else{
                alert("电子邮件格式不正确\n"+"必须包含@符号和.符号！");
                return false;
            }
        }
        else{
            alert("请输入电子邮件！")
            return false;
        }
    }


     function validateform(){
        if(passCheck( )&&checkEmail()&&codeCheck())
            return true;
        else
           return false;
    }

    function clearText( ) {
        document.myform.user.value="" ;
        document.myform.password.value="" ;
    }


	function sendCode(){
	
		if(!checkEmail())return;
		var email = document.getElementById("email").value;
		$.ajax({
			type: 'post',
			url: '/api/code/create/'+email,
			success: function(data){
				if(typeof data=="string"){
                 data=JSON.parse(data);
                }
				if(data.code=="200"){
				  showtime(60);
				}else if(data.code=="301"){
                     location.href="/welcome.html";
                }else{
                    alert(data.msg);
                }
			}
		});
	}


	function showtime(t){ 
            document.myform.buttonCode.disabled=true; 
             for(i=0;i<=t;i++) { 
             window.setTimeout("update_p(" + i + ","+t+")", i *1000 ); 
             } 

        } 
        function update_p(num,t) { 
             if(num == t) { 
                 document.myform.buttonCode.value =" 重新发送 "; 
                 document.myform.buttonCode.disabled=false; 
             } else { 
                 printnr = t-num; 
                document.myform.buttonCode.value = " (" + printnr +")秒后重新发送"; 
             } 
        } 


	//显示隐藏对应的switchPwd()方法:
	$(function(){
        // 通过jqurey修改
        $("#passwordeye").click(function(){
            let type =  $("#password").attr('type')
            if(type === "password"){
                $("#password").attr("type","text");
            }else{
                $("#password").attr("type","password");
            }
        });
		 
	});


	function register(){
		var valid=validateform();
		if(valid){
		var email = document.getElementById("email").value;
		var code = document.getElementById("code").value;
		var password = document.getElementById("password").value;
		$.ajax({
			type: 'post',
			url: '/api/user/register/'+email+'/'+password+'/'+code,
			success: function(data){
				if(typeof data=="string"){
                 data=JSON.parse(data);
                }
				if(data.code=="200"){
					alert("注册成功，请登录")
				}else if(data.code=="301"){
                     location.href="/welcome.html";
                }else{
                    alert(data.msg);
                }
			}
		});
	}
	}



</SCRIPT>
</head>
<body>
<!-- <img src="img/login_bg.jpg" class="bg"> -->
<div id="head">
	<!-- <img src="img/register_head.png" width=100% height=auto /> -->
</div>
<div id="center">
<!-- <form name="myform" onSubmit="return validateform( )" enctype="multipart/form-data" action="/test/123" method="post" target="framename" > -->
	<form name="myform">

	<div class="input-group">
        <h3>邮&nbsp;&nbsp;&nbsp;&nbsp;箱：</h3><input class="form-control"  id="email" name="email" type="text"  style="height:40px" value=""  placeholder="请输入邮箱"/>
	</div>
    <div class="input-group">
        <h3>验&nbsp;&nbsp;证&nbsp;&nbsp;码：</h3><input class="form-control"  id="code" name="code" type="text"  style="height:40px" value=""  placeholder="请输入验证码"/>
        <input  class="btn btn-primary" id="buttonCode"  type="button" value="获取验证码" onclick="sendCode()"/>
    </div>		
	<div class="input-group">
        <h3>密&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;码：</h3><input class="form-control"  id="password" name="password" type="password" style="height:40px" value=""  placeholder="密码长度6-12位"/>
		<span class="input-group-btn">
			<INPUT class="btn btn-default" id="passwordeye" type="button" value="show/hide"">
        </span>
    </div>
	  
	<div id="btn">
		<INPUT name="registerButton"  class="btn btn-primary" type="button" id="Button" value="注册"  onclick="register()">
		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;	
		<a href="login.html"><INPUT  class="btn btn-primary" name="loginButton" id="Button" type="button" value="登录"></a>
	</div>
</form>
</div>
<div id="bottom">
	<div class="footer" style="color:white;">
     	Copyright &copy; 2013-2019 All Rights Reserved. 备案号：
    </div>
</div>
</body>
</html>
